/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.multi.client.account.service.config;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.medusa.gruul.common.core.auth.client.helper.ISecurity;
import com.medusa.gruul.common.core.auth.model.UserExtends;
import com.medusa.gruul.common.core.helper.ISecurityUserService;
import com.medusa.gruul.multi.client.account.api.entity.MultiClientAccount;
import com.medusa.gruul.multi.client.account.service.helper.UserHelper;
import com.medusa.gruul.multi.client.account.service.mp.service.IMultiClientAccountService;
import com.medusa.gruul.multi.client.account.service.properties.MultiClientProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @author 张治保
 * date 2021/11/30
 */
@Configuration
@EnableConfigurationProperties(MultiClientProperties.class)
@RequiredArgsConstructor
public class AccountConfig {

    private final MultiClientProperties multiClientProperties;
    private final IMultiClientAccountService multiClientAccountService;
//    @Bean
//    public CorsFilter corsFilter() {
//        CorsConfiguration config = new CorsConfiguration();
//        config.addAllowedOrigin("*");
//        config.setAllowCredentials(true);
//        config.addAllowedMethod("*");
//        config.addAllowedHeader("*");
//        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
//        configSource.registerCorsConfiguration("/**", config);
//        return new CorsFilter(configSource);
//    }

    @Bean
    public Snowflake userIdSnowflake(){
        MultiClientProperties.Snowflake userIdSnowflake = multiClientProperties.getUserIdSnowflake();
        return IdUtil.createSnowflake(
            userIdSnowflake.getWorkerId(),
            userIdSnowflake.getDatacenterId()
        );
    }

    @Bean
    public Snowflake shopUserIdSnowflake(){
        MultiClientProperties.Snowflake shopUserIdSnowflake = multiClientProperties.getShopUserIdSnowflake();
        return IdUtil.createSnowflake(
            shopUserIdSnowflake.getWorkerId(),
            shopUserIdSnowflake.getDatacenterId()
        );
    }

    @Bean
    public ISecurityUserService securityUserService(){
        return (accountId)-> {
            MultiClientAccount account = multiClientAccountService.getAccountById(accountId);
            return UserHelper.getTokenByAccount(
                account,new UserExtends()
            );
        };
    }

    @Bean(name = "userIdGenerator")
    public KeyGenerator userIdGenerator(){
        return (target,method,params)-> ISecurity.user().must().getUserId();
    }

}
